package com.cams.backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cams.backend.common.lang.Result;
import com.cams.backend.entity.QueryDate;
import com.cams.backend.entity.Record;
import com.cams.backend.entity.Users;
import com.cams.backend.entity.Work;
import com.cams.backend.mapper.RecordMapper;
import com.cams.backend.mapper.UsersMapper;
import com.cams.backend.mapper.WorkMapper;
import com.cams.backend.service.RecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cams
 * @since 2022-05-15
 */
@Service
public class RecordServiceImpl extends ServiceImpl<RecordMapper, Record> implements RecordService {

    @Resource
    RecordMapper recordMapper;

    @Resource
    UsersMapper usersMapper;

    @Resource
    WorkMapper workMapper;

    @Override
    public Result onDuty(Record record) {
        LocalDateTime rtime = LocalDateTime.now();
        LocalDate rdate=rtime.toLocalDate();
        int year = rdate.getYear();
        int month = rdate.getMonthValue();
        int date = rdate.getDayOfMonth();

        record.setRtime(rtime);
        record.setRtype("上班");

        Users user = usersMapper.selectOne(new QueryWrapper<Users>().eq("uid", record.getUid()));
        record.setUname(user.getName());

        LocalDateTime endTime = LocalDateTime.of(year,month,date,9,00,00);
        if(rtime.isBefore(endTime)){
            record.setRstate("正常");
        }
        else{
            record.setRstate("迟到");
        }

        //判断今天是否被排班

        QueryWrapper<Work> requestWorkqw=new QueryWrapper<Work>();
        requestWorkqw
                .eq("uid",record.getUid())
                .ne("wsdate",rdate)
                .ge("wedate",rdate)
        ;
        Work requestWork = workMapper.selectOne(requestWorkqw);

        if(requestWork==null){

            return Result.fail("该用户今日未排班");

        }
        else{
            //判断今天是否已经上班打卡过
            LocalDate nowDate=LocalDate.now();
            LocalDateTime stime= nowDate.atTime(00,00,00);
            LocalDateTime etime= nowDate.atTime(23,59,59);
            QueryWrapper<Record> queryWrapper1=new QueryWrapper<Record>();
            queryWrapper1
                    .eq("uid",record.getUid())
                    .eq("rtype","上班")
                    .between("rtime",stime,etime)
            ;
            Record temp1 = recordMapper.selectOne(queryWrapper1);

            QueryWrapper<Record> queryWrapper2=new QueryWrapper<Record>();
            queryWrapper2
                    .eq("uid",record.getUid())
                    .eq("rtype","请假")
                    .between("rtime",stime,etime)
            ;
            Record temp2 = recordMapper.selectOne(queryWrapper2);

            if(temp1==null && temp2==null){
                recordMapper.insert(record);

                return Result.succeed(record);
            }
            else if (temp1!=null){
                return Result.fail("该用户今日已上班打卡");
            }
            else if(temp2!=null){
                return Result.fail("该用户今日已请假");
            }
            else{
                return Result.fail("打卡失败");
            }
        }




    }

    @Override
    public Result offDuty(Record record) {
        LocalDateTime rtime = LocalDateTime.now();
        LocalDate rdate=rtime.toLocalDate();
        int year = rdate.getYear();
        int month = rdate.getMonthValue();
        int date = rdate.getDayOfMonth();
        record.setRtime(rtime);
        record.setRtype("下班");

        Users user = usersMapper.selectOne(new QueryWrapper<Users>().eq("uid", record.getUid()));
        record.setUname(user.getName());

        LocalDateTime endTime = LocalDateTime.of(year,month,date,18,00,00);
        if(rtime.isBefore(endTime)){
            record.setRstate("早退");
        }
        else{
            record.setRstate("正常");
        }


        //判断今天是否已经上班打卡过
        LocalDate nowDate=LocalDate.now();
        LocalDateTime stime= nowDate.atTime(00,00,00);
        LocalDateTime etime= nowDate.atTime(23,59,59);
        QueryWrapper<Record> ondutyqw=new QueryWrapper<Record>();
        ondutyqw
                .eq("uid",record.getUid())
                .eq("rtype","上班")
                .between("rtime",stime,etime)
        ;
        Record onduty = recordMapper.selectOne(ondutyqw);
        QueryWrapper<Record> offdutyqw=new QueryWrapper<Record>();
        offdutyqw
                .eq("uid",record.getUid())
                .eq("rtype","下班")
                .between("rtime",stime,etime)
        ;
        Record offduty = recordMapper.selectOne(offdutyqw);
        if(onduty==null){

            return Result.fail("今日还未上班打卡！");
        }
        else{
            if(offduty==null){
                recordMapper.insert(record);

                return Result.succeed(record);
            }
            else{
                return Result.fail("该用户今日已下班打卡");
            }
        }

    }

    @Override
    public Result findByUid(int uid){
        List<Record> recordList = recordMapper.selectList(new QueryWrapper<Record>().eq("uid",uid));
        for (int i = 0; i < recordList.size(); i++) {
            System.out.println(recordList.get(i));
            System.out.println(recordList.get(i).getClass());
            System.out.println("==");
        }
        return Result.succeed(recordList);
    }

    @Override
    public Result findByRtime(QueryDate queryDate) {
        LocalDate rdate=queryDate.getDate();
        LocalDateTime stime= rdate.atTime(00,00,00);
        LocalDateTime etime= rdate.atTime(23,59,59);
        QueryWrapper<Record> queryWrapper=new QueryWrapper<Record>()
                .between("rtime",stime,etime);
        List<Record> recordList = recordMapper.selectList(queryWrapper);
        return  Result.succeed(recordList);
    }

    @Override
    public Result getOverTime(int uid, String rtype) {
        QueryWrapper<Record> queryWrapper=new QueryWrapper<Record>().eq("uid",uid).eq("rtype",rtype);
        List<Record> recordList = recordMapper.selectList(queryWrapper);
        return Result.succeed(recordList);
    }

}
